<?php
// http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-x-x-which-uses-graph-api/

	include_once('./inc/config.php');
	error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
	
//-----------------------------------------------------------------
//            The first part is Application Configurations.
//-----------------------------------------------------------------
$app_id		= "";
$app_secret	= "6ffa876c61b4f7538038771d14950889";
$site_url	= "http://www.rosarioalojamientos.com";

echo "A<br>";
//-----------------------------------------------------------------
// Now, include ‘facebook.php’ which can be found in the ‘src’ folder of PHP SD that you downloaded.
// Then you need to create an application instance using the constructor of class ‘Facebook’. 
// The App ID and App Secret need to be passed as arguments for the constructor. 
// In earlier versions of PHP SDK (v2.x.x), the argument array had an optional key ‘session’. But in v3.x.x, ‘session’ key is not used at all.
// Then we call the function getUser() which will return the user ID. 
// We will be using a variable ‘$user’ to store the User ID of Facebook user. 
// We may or may not have this data based on whether the user is logged in. 
// If we have a $user id here, it means we know the user is logged into Facebook, but we don’t know if the access token is valid. 
// An access token is invalid if the user logged out of Facebook.
//-----------------------------------------------------------------

include(ROOT."/modules/fblogin/src/facebook.php");
echo "B<br>"; 
$facebook = new Facebook(array(
	'appId'		=> $app_id,
	'secret'	=> $app_secret,
	));
 
$user = $facebook->getUser();
echo "C<br>";

if($user){
	echo "C1<br>";
	//==================== Single query method ======================================
	try{
		// Proceed knowing you have a logged in user who's authenticated.
		$user_profile = $facebook->api('/me');
	}catch(FacebookApiException $e){
		error_log($e);
		$user = NULL;
	}
	//==================== Single query method ends =================================
}
 
echo "D<br>"; 
if($user){
	echo "d1<br>";
	// Get logout URL
	$logoutUrl = $facebook->getLogoutUrl();
}else{
	echo "d2<br>";
	// Get login URL
	$loginUrl = $facebook->getLoginUrl(array(
		'scope'		=> 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
		'redirect_uri'	=> $site_url,
		));
		
		
}
echo "E<br>"; 
if($user){
	echo "e1<br>";
	// Proceed knowing you have a logged in user who has a valid session.
 
//========= Batch requests over the Facebook Graph API using the PHP-SDK ========
	// Save your method calls into an array
	$queries = array(
		array('method' => 'GET', 'relative_url' => '/'.$user),
		array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
		array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
		array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),
		);
 
	// POST your queries to the batch endpoint on the graph.
	try{
		$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
	}catch(Exception $o){
		error_log($o);
	}
 
	//Return values are indexed in order of the original array, content is in ['body'] as a JSON
	//string. Decode for use as a PHP array.
	$user_info		= json_decode($batchResponse[0]['body'], TRUE);
	$feed			= json_decode($batchResponse[1]['body'], TRUE);
	$friends_list	= json_decode($batchResponse[2]['body'], TRUE);
	$photos			= json_decode($batchResponse[3]['body'], TRUE);
//========= Batch requests over the Facebook Graph API using the PHP-SDK ends =====
 
	// Update user's status using graph api
	if(isset($_POST['publish'])){
		try{
			$publishStream = $facebook->api("/$user/feed", 'post', array(
				'message'		=> 'Check out 25 labs',
				'link'			=> 'http://25labs.com',
				'picture'		=> 'http://25labs.com/images/25-labs-160-160.jpg',
				'name'			=> '25 labs',
				'caption'		=> '25labs.com',
				'description'		=> 'A Technology Laboratory. Highly Recomented technology blog.',
				));
		}catch(FacebookApiException $e){
			error_log($e);
		}
	}
 
	// Update user's status using graph api
	if(isset($_POST['status'])){
		try{
			$statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['status']));
		}catch(FacebookApiException $e){
			error_log($e);
		}
	}
	pr($user_info);
	
	
}








	
?>